/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysUserDao.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月20日
 */
package com.sirdc.modules.sys.dao;

import java.util.List;

import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.utils.ObjectUtils;
import net.huashitong.supervision.task.entity.TaskUnit;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.sirdc.modules.core.dao.GenericQuery;
import com.sirdc.modules.core.dao.StringDao;
import com.sirdc.modules.sys.entity.SysUser;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.utils.CollectionsUtils;
import com.sirdc.modules.utils.StringUtils;

/**
 * 
 * @version Revision: 0.0.1
 */
@Repository
public class SysUserDao extends StringDao<SysUser> {

	public List<SysUser> query(final SysUserFilter filter) {
		GenericQuery query = createQuery("obj");
		if (StringUtils.isNotBlank(filter.getName())) {
			if(1==filter.getNameNotLike()){
				query.append(" and obj.name =:name");
				query.setParam("name", filter.getName());

			}else {

				query.append(" and obj.name like :name");
				query.setParam("name", StringUtils.getLikewords(filter.getName()));

			}
		}
		
		if (CollectionsUtils.isNotEmpty(filter.getInIds())) {
			query.append(" and obj.sysId in (:ins)");
			query.setParamList("ins", filter.getInIds());
		}
		
		if (CollectionsUtils.isNotEmpty(filter.getNotInIds())) {
			query.append(" and obj.sysId not in (:notins)");
			query.setParamList("notins", filter.getNotInIds());
		}
		
		if (StringUtils.isNotBlank(filter.getTeachClass())) {
			query.append(" and obj.teachClass = :teachClass");
			query.setParam("teachClass", filter.getTeachClass());
		}
		if (StringUtils.isNotBlank(filter.getMobile())) {
			query.append(" and obj.mobile like :mobile");
			query.setParam("mobile", StringUtils.getLikewords(filter.getMobile()));
		}
		query.append(" and obj.state = :state").setParam("state", "01");
		
		//如果不是超级管理员，则不显示超级管理员用户
		if(!filter.isSuperAdmin()) {
			query.append(" and obj.superAdmin = :superAdmin");
			query.setParam("superAdmin", false);
		}
		return query.listResult(filter);
	}


	public List<SysUser> queryList(final SysUserFilter filter) {
		GenericQuery query = createQuery("obj");
		if (StringUtils.isNotBlank(filter.getName())) {
			query.append(" and obj.name like :name");
			query.setParam("name", StringUtils.getLikewords(filter.getName()));
		}

		if (StringUtils.isNotBlank(filter.getMobile())) {
			query.append(" and obj.mobile like :mobile");
			query.setParam("mobile", StringUtils.getLikewords(filter.getMobile()));
		}
		query.append(" and obj.state = :state").setParam("state", "01");

		//如果不是超级管理员，则不显示超级管理员用户
		if(!filter.isSuperAdmin()) {
			query.append(" and obj.superAdmin = :superAdmin");
			query.setParam("superAdmin", false);
		}
		return query.listResult();
	}
	/**
	 * 批量删除用户(帐号停用)
	 * @author: weihuang.peng
	 * @param ids
	 */
	public void batchDeleteUser(List<String> ids) {
		if (CollectionsUtils.isNotEmpty(ids)) {
			GenericQuery query = create("update SysUser user set user.state=:state").setParam("state", "03");
			query.append(" where user.id in (:ids)").setParamList("ids", ids);
			query.append(" and user.superAdmin != :superAdmin").setParam("superAdmin", true);
			query.executeUpdate();
		}
	}

	/**
	 * 统计一个教学班的人数
	 * @author: huiyang.yu
	 * @param teachClass
	 * @return
	 */
	public Integer queryCountByTeachClass(String teachClass) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.teachClass =:teachClass").setParam("teachClass", teachClass);
		return query.count();
	}

	public List<SysUser> getByInName(String[] names) {
		GenericQuery query = createQuery("obj");
		query.append(" and obj.name in (:names)").setParam("names", names);
		return query.listResult();
	}
	public List<SysUser> queryGet(List<String> ids){
		GenericQuery query = createQuery("obj");
		query.append(" and obj.sysId in (:ids)").setParam("ids", ids);
		return query.listResult();
	}
	public List<SysUser> queryFilter(SysUserFilter filter){
		StringBuilder sql = new StringBuilder();
		sql.append("select t1.sys_id as sysId, t1.name as name,t1.mobile as mobile,t2.role_id as idCard,t1.position as position,t1.state as state from t_sys_user t1,t_sys_login t2 where 1=1");
		if(StringUtils.isNotBlank(filter.getDeptId())){
			sql.append(" and t2.dept_sys_id = '"+filter.getDeptId()+"'");
		}
		if (StringUtils.isNotBlank(filter.getName())){
			sql.append(" and t1.name like '%"+filter.getName()+"%'");
		}
		if (StringUtils.isNotBlank(filter.getRoleId())){
			sql.append(" and t2.role_id = '"+filter.getRoleId()+"'");
		}
        if (StringUtils.isNotBlank(filter.getMobile())){
            sql.append(" and t1.mobile like '%"+filter.getMobile()+"%'");
        }
		sql.append(" and t1.sys_id = t2.sys_id");
        sql.append(" and t1.state = '01'");

        // 设置查询总条数,用于分页
		if (ObjectUtils.isNotBlank(filter.getPaging())) {

			int resultSize = this.countUser(filter);
			filter.getPaging().setRecords(resultSize);
			//设置排序
			if (ObjectUtils.isNotBlank(filter.getPaging())) {
				if (StringUtils.isBlank(filter.getPaging().getSortCol())) {
					filter.getPaging().setSortCol("t2.user_sort");
					filter.getPaging().setSortVal("ASC");
				}
				sql.append(" order by " + filter.getPaging().getSortCol() + " " + filter.getPaging().getSortVal());
			}

			SQLQuery query = getSession().createSQLQuery(sql.toString());

			query.setFirstResult(filter.getPaging().getPageSize() * (filter.getPaging().getCurrentPage() - 1));
			query.setMaxResults(filter.getPaging().getPageSize());
			query.setResultTransformer(Transformers.aliasToBean(SysUser.class));
			List<SysUser> list = query.list();
			return list;
		} else {
			SQLQuery query = getSession().createSQLQuery(sql.toString());
			query.setResultTransformer(Transformers.aliasToBean(SysUser.class));
			List<SysUser> list = query.list();
			return list;
		}
	}

	public List<SysUser> queryFilterNoPage(SysUserFilter filter){
		StringBuilder sql = new StringBuilder();
		sql.append("select t1.sys_id as sysId, t1.name as name,t1.mobile as mobile,t2.role_id as idCard,t1.position as position,t1.state as state from t_sys_user t1,t_sys_login t2 where 1=1");
		if(StringUtils.isNotBlank(filter.getDeptId())){
			sql.append(" and t2.dept_sys_id = '"+filter.getDeptId()+"'");
		}
		if (StringUtils.isNotBlank(filter.getName())){
			sql.append(" and t1.name like '%"+filter.getName()+"%'");
		}
		if (StringUtils.isNotBlank(filter.getRoleId())){
			sql.append(" and t2.role_id = '"+filter.getRoleId()+"'");
		}
		if (StringUtils.isNotBlank(filter.getMobile())){
			sql.append(" and t1.mobile like '%"+filter.getMobile()+"%'");
		}
		sql.append(" and t1.sys_id = t2.sys_id");
		sql.append(" and t1.state = '01'");
		sql.append(" order by t2.user_sort asc");
		SQLQuery query = getSession().createSQLQuery(sql.toString());
        query.setResultTransformer(Transformers.aliasToBean(SysUser.class));
		List<SysUser> list = query.list();
		return list;
	}

	private int countUser(SysUserFilter filter) {
		StringBuilder sql = new StringBuilder();
		sql.append("select count(t1.sys_id) from t_sys_user t1,t_sys_login t2 where 1=1");
		if(StringUtils.isNotBlank(filter.getDeptId())){
			sql.append(" and t2.dept_sys_id = '"+filter.getDeptId()+"'");
		}
		if (StringUtils.isNotBlank(filter.getName())){
			sql.append(" and t1.name like '%"+filter.getName()+"%'");
		}
		if (StringUtils.isNotBlank(filter.getRoleId())){
			sql.append(" and t2.role_id = '"+filter.getRoleId()+"'");
		}
		sql.append(" and t1.sys_id = t2.sys_id");
        sql.append(" and t1.state = '01'");


        SQLQuery query = getSession().createSQLQuery(sql.toString());
		return ((Number) query.uniqueResult()).intValue();
	}

	public List<SysUser> getByMobile(String mobile) {
		GenericQuery query = createQuery("obj");
		query.append(" and mobile=:mobile").setParam("mobile",mobile );
		return query.listResult();
	}

	/**
	 * 根据名字查找
	 * @param name
	 * @return
	 */
	public List<SysUser> getByName(String name) {
		GenericQuery query = createQuery("obj");
		query.append(" and name=:name").setParam("name",name );
		return query.listResult();
	}

	public List<SysUser> getByDeptId(String deptId) {
		StringBuilder sql = new StringBuilder();
		sql.append("select t1.sys_id as sysId, t1.name as name,t1.mobile as mobile,t2.role_id as idCard,t1.position as position,t1.state as state from t_sys_user t1,t_sys_login t2 where 1=1");
		if(StringUtils.isNotBlank(deptId)){
			sql.append(" and t2.dept_sys_id = '"+deptId+"'");
		}
		sql.append(" and t1.sys_id = t2.sys_id");
		sql.append(" and t1.state = '01'");
		SQLQuery query = getSession().createSQLQuery(sql.toString());
		query.setResultTransformer(Transformers.aliasToBean(SysUser.class));
		List<SysUser> list = query.list();
		return list;
	}


	public List<SysUser> getUserByDept(SysUserFilter filter)
	{

		String hql = "select z " +
				"     from  SysUser as z ,SysLogin as g " +
				"      where z.sysId=g.sysId ";
		GenericQuery query =create(hql);
		query.append(" and g.deptId=:deptId").setParam("deptId",filter.getDeptId());
		if(StringUtils.isNotBlank(filter.getPersonSignal()))
		{

			query.append(" and z.personSignal=:personSignal").setParam("personSignal",filter.getPersonSignal());
		}


		return query.listResult();

	}
}
